package com.example.demo.controller;

import com.example.demo.entity.ResultEntity;
import com.example.demo.service.serviceimpl.PurchaseHistoryServiceImpl;
import com.example.demo.vo.PurchaseHistoryVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/purchaseHistory/")
@CrossOrigin
@RestController
public class PurchaseHistoryController {
    @Resource
    PurchaseHistoryServiceImpl purchaseHistoryService;

    // 根据用户ID获取购买历史
    @GetMapping("getUserPurchaseHistory")
    private ResultEntity getUserPurchaseHistory(@RequestParam("user_id") Integer user_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(purchaseHistoryService.getUserPurchaseHistory(user_id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取购买历史失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据ID获取购买历史
    @GetMapping("getPurchaseHistoryById")
    private ResultEntity getPurchaseHistoryById(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(purchaseHistoryService.getPurchaseHistoryById(id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取购买历史失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 获取所有购买历史
    @GetMapping("getAllPurchaseHistories")
    private ResultEntity getAllPurchaseHistories() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(purchaseHistoryService.getAllPurchaseHistories());
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取所有购买历史失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 添加购买历史
    @PostMapping("addPurchaseHistory")
    private ResultEntity addPurchaseHistory(@RequestBody PurchaseHistoryVo purchaseHistoryVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (purchaseHistoryService.addPurchaseHistory(purchaseHistoryVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("添加购买历史成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("添加购买历史失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 更新购买历史
    @PutMapping("updatePurchaseHistory")
    private ResultEntity updatePurchaseHistory(@RequestBody PurchaseHistoryVo purchaseHistoryVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (purchaseHistoryService.updatePurchaseHistory(purchaseHistoryVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("更新购买历史成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("更新购买历史失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据ID删除购买历史
    @DeleteMapping("deletePurchaseHistory/{id}")
    private ResultEntity deletePurchaseHistory(@PathVariable("id") int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (purchaseHistoryService.deletePurchaseHistory(id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除购买历史成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("删除购买历史失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
}